package com.example.myapplication2003.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication2003.R;
import com.example.myapplication2003.bean.Product;

import java.util.List;
import java.util.zip.Inflater;

// 自定义的adapter,
public class CartAdapter extends ArrayAdapter<Product> {
    public CartAdapter(@NonNull Context context, int resource, @NonNull List<Product> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    // 返回每一行渲染的内容
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Product product = getItem(position);   // 每一行的数据
        View view = LayoutInflater.from(getContext()).inflate(R.layout.cart_listview_item, parent, false);
        ImageView imageView = view.findViewById(R.id.image);
        TextView nameView = view.findViewById(R.id.name);
        TextView typeView = view.findViewById(R.id.type);
        imageView.setImageResource(product.getImage());
        nameView.setText(product.getName());

        if (product.getType() == 0) {
            typeView.setVisibility(View.INVISIBLE); // 隐藏typeView
        } else {
            typeView.setText("自营");
        }

        return view;
    }
}
